import time

from selenium import webdriver
from selenium.webdriver.common.by import By

#显式等待导包
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#鼠标事件导包
from selenium.webdriver.common.action_chains import ActionChains
#键盘事件导包
from selenium.webdriver import Keys
driver=webdriver.Firefox()
driver.get('http://10.59.9.12:7001/')
driver.maximize_window()
time.sleep(3)

# 第一个业务用户搜索商品并购买
# 登录成功
#鼠标单击事件
sbdj=driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a')
time.sleep(3)
#单击
ActionChains(driver).click(sbdj).perform()
# #双击
# ActionChains(driver).double_click(sbdj).perform()
time.sleep(5)
driver.find_element(By.NAME,'username').send_keys('13811033962')
time.sleep(3)
driver.find_element(By.NAME,'password').send_keys('123456')
time.sleep(10)
#点击登录按钮
driver.find_element(By.XPATH,'//*[@id="theForm"]/div[5]/a').click()
time.sleep(3)

#切换窗口
driver.switch_to.window(driver.window_handles[-1])
time.sleep(5)
# # 商品搜索
# driver.find_element(By.CLASS_NAME,'search-input').send_keys('音乐耳机')
# time.sleep(3)

# 搜索包含键盘事件
driver.find_element(By.CLASS_NAME,'search-input').send_keys('音乐耳机')
# #点击搜索
# driver.find_element(By.CLASS_NAME,'search-input-btn').click()

#键盘事件回车
a=driver.find_element(By.CLASS_NAME,'search-input')
a.send_keys(Keys.ENTER)

# 高级定位
# driver.find_element(By.XPATH,'//input[contains(@class,"search-input-b")]')
# time.sleep(3)
# driver.find_element(By.XPATH,'//input[@class="search-input-btn"]')
# time.sleep(3)
#隐式等待
driver.implicitly_wait(15)
#商品详情
driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/ul/li/div[1]/span/a/img').click()
time.sleep(3)
#打印元素
dy=driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/ul/li/div[1]/span/a/img')
print(dy.text)
print(dy.size)
print(dy.location)
print(dy.get_attribute('class'))
#立即购买
#显示等待
buy=WebDriverWait(driver,30,0.5).until(EC.presence_of_element_located((By.CLASS_NAME,'shop-btn')))
buy.click()
# driver.find_element(By.CLASS_NAME,'shop-btn').click()
# time.sleep(3)
# 创建订单并支付
driver.find_element(By.ID,'address-801').click()
time.sleep(3)
driver.find_element(By.CSS_SELECTOR,'#order-button').click()
time.sleep(3)

#滑动事件
driver.execute_script('window.scrollTo(0,1000)')


# #第二个业务用户浏览商品列表并加入购物车结算
# # 登录成功
# driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
# time.sleep(3)
# driver.find_element(By.NAME,'username').send_keys('13811033962')
# time.sleep(3)
# driver.find_element(By.NAME,'password').send_keys('123456')
# time.sleep(10)
# driver.find_element(By.XPATH,'//*[@id="theForm"]/div[5]/a').click()
# time.sleep(3)
# #切换窗口
# driver.switch_to.window(driver.window_handles[-1])
# time.sleep(5)
# # 首页商品列表
# driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/ul/li[1]/a').click()
# time.sleep(3)
# # 商品详情
# driver.find_element(By.XPATH,'/html/body/div[8]/div[2]/div[2]/div[2]/ul/li[2]/div[1]/a/img').click()
# time.sleep(5)
# # 加入购物车
# driver.find_element(By.ID,'add-cart').click()
# # driver.find_element(By.LINK_TEXT('加入购物车')).click()
# time.sleep(5)
# #进入购物车
# driver.find_element(By.XPATH,'/html/body/div[3]/div[3]/div[2]/div/div/div[2]/a').click()
# time.sleep(5)
# #选择商品
# driver.find_element(By.NAME,'id[]').click()
# time.sleep(3)
# #立即结算
# driver.find_element(By.CSS_SELECTOR,'body > div.navigation-wrap > div.nav-wrap > ul > li:nth-child(1) > a').click()
# time.sleep(3)
# # 创建订单并支付
# driver.find_element(By.ID,'address-801').click()
# time.sleep(3)
# driver.find_element(By.CSS_SELECTOR,'#order-button').click()
# time.sleep(3)